package cn.b.sky.mes.andon.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.andon.model.AndonAttribute;
import cn.b.sky.mes.andon.model.AndonTemplate;
import cn.b.sky.mes.andon.model.condition.AndonTemplateCondition;
import cn.b.sky.mes.andon.service.AndonAttributeService;
import cn.b.sky.mes.andon.service.AndonTemplateService;
import cn.b.sky.mes.andon.service.AndonTypeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * 
 * <strong>Title : AndonTemplateController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.andon.controller<br></strong>
 * <strong>Description : </strong>暗灯模板操作控制器<br> 
 * <strong>Create on : 2016-10-14 下午01:19:04<br></strong>
 * <p>
 * @author JunLin.Yang<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/andonTemplate/*")
public class AndonTemplateController extends SecurityController {

  @Resource
  AndonTemplateService andonTemplateService;
  
  @Resource
  AndonAttributeService andonAttributeService;
  
  @Resource
  AndonTypeService andonTypeService;

  /**
   * 
   * <strong>Title : listAndonTemplatesPageInfo<br></strong>
   * <strong>Description : </strong>根据查询条件查询暗灯模板分页信息<br> 
   * <strong>Create on : 2016-10-14 下午01:19:04<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param condition 暗灯模板查询条件
   * @param model 页面值对象
   * @return
   * String 页面地址
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "listAndonTemplatesPageInfo")
  public String listAndonTemplatesPageInfo(String navTabId,
      @ModelAttribute("condition") AndonTemplateCondition condition, Model model) {
    String siteCode = this.getSiteCode();

    model.addAttribute(NAVTABID, navTabId);

    // 添加SITECODE条件值并查询分页信息
    condition.setSiteCode(siteCode);
    
    if(StringUtils.isBlank(condition.getOrderField())){
      condition.setOrderField("CREATEDATETIME");
    }
    
    model.addAttribute("page", this.andonTemplateService.selectAndonTemplatePageByCondition(condition));
    model.addAttribute("andonTypeList", this.andonTypeService.getAndonAccountListBySiteCode(siteCode,0));

    return "andon/andonTemplate/andonTemplate_list";
  }

  /**
   * 
   * <strong>Title : viewAndonTemplate<br></strong>
   * <strong>Description : </strong>根据主键查询暗灯模板对象<br> 
   * <strong>Create on : 2016-10-14 下午01:19:04<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param siteCode 工厂代码
   * @param templateCode 暗灯模板编号
   * @param model 页面值对象
   * @return
   * String 页面地址
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "viewAndonTemplate")
  public String viewAndonTemplate(String navTabId, String templateCode, Model model) {
    String siteCode = this.getSiteCode();

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("andonTemplate", this.andonTemplateService.getAndonTemplateAndAttributesByPrimaryKey(siteCode, templateCode));

    return "andon/andonTemplate/andonTemplate_view";
  }

  /**
   * 
   * <strong>Title : toAddAndonTemplate<br></strong>
   * <strong>Description : </strong>新增暗灯模板信息前置事件<br> 
   * <strong>Create on : 2016-10-14 下午01:19:04<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param model 页面值对象
   * @return
   * String 页面地址
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "toAddAndonTemplate")
  public String toAddAndonTemplate(String navTabId, Model model) {

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("andonTypeList", this.andonTypeService.getAndonAccountListBySiteCode(this.getSiteCode(),0));
    return "andon/andonTemplate/andonTemplate_edit";
  }

  /**
   * 
   * <strong>Title : toEditAndonTemplate<br></strong>
   * <strong>Description : </strong>修改暗灯模板信息前置事件<br> 
   * <strong>Create on : 2016-10-14 下午01:19:04<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param siteCode 工厂代码
   * @param templateCode 暗灯模板编号
   * @param model 页面值对象
   * @return
   * String 页面地址
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "toEditAndonTemplate")
  public String toEditAndonTemplate(String navTabId, String templateCode, Model model) {

    String siteCode = this.getSiteCode();

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("andonTemplate",this.andonTemplateService.getAndonTemplateAndAttributesByPrimaryKey(siteCode, templateCode) );
    model.addAttribute("andonTypeList", this.andonTypeService.getAndonAccountListBySiteCode(siteCode,0));
    return "andon/andonTemplate/andonTemplate_edit";
  }

  /**
   * 
   * <strong>Title : saveOrUpdateAndonTemplate<br></strong>
   * <strong>Description : </strong>新增或修改暗灯模板信息<br> 
   * <strong>Create on : 2016-10-14 下午01:19:04<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param andonTemplate 暗灯模板
   * @param model 页面值对象
   * @return
   * DWZResult 操作结果
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "saveOrUpdateAndonTemplate")
  @ResponseBody
  public DWZResult saveOrUpdateAndonTemplate(String navTabId, AndonTemplate andonTemplate, Model model) {
    if (null == andonTemplate) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }

    andonTemplate.setSiteCode(getSiteCode());
    setCreateInfo(andonTemplate);

    if(null == andonTemplate.getAlarmFlag()){
      andonTemplate.setAlarmFlag(0);
    }
    
    if(null == andonTemplate.getLockFlag()){
      andonTemplate.setLockFlag(0);
    }
    
    this.andonTemplateService.insertOrUpdateAndonTemplateAndAttributes(andonTemplate);
    
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
  }
  
  /**
   * 
   * <strong>Title : listAndonAttributesByAndonType<br></strong>
   * <strong>Description : </strong>根据ANDON类型获取相关自定义属性值集合信息<br> 
   * <strong>Create on : 2016年10月17日 上午10:27:44<br></strong>
   * <p>
   * @param navTabId 页面TABID
   * @param andonType ANDON类型
   * @param model 数据模型
   * @return
   * DWZResult 操作结果
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "listAndonAttributesByAndonType")
  @ResponseBody
  public  DWZResult listAndonAttributesByAndonType(String navTabId, String andonType, Model model){
    if (StringUtils.isBlank(andonType)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    List<AndonAttribute> andonAttributeList = this.andonAttributeService.selectAndonAttributeAndEnumsListByAndonType(this.getSiteCode(), andonType);
    
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, andonAttributeList);
  }

  /**
   * 
   * <strong>Title : deleteAndonTemplateBatch<br></strong>
   * <strong>Description : </strong>删除暗灯模板及其自定义属性值信息<br> 
   * <strong>Create on : 2016-10-14 下午01:19:04<br></strong>
   * <p>
   * @param navTabId 页面导航ID
   * @param model 页面值对象
   * @param request 页面请求对象
   * @return
   * DWZResult 操作结果
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "deleteAndonTemplateBatch")
  @ResponseBody
  public DWZResult deleteAndonTemplateBatch(String navTabId, Model model, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isEmpty(ids)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String siteCode = this.getSiteCode();
    String[] ress = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(ress.length);
    boolean hasFail = false;
    BatchJobReport report = null;
    for (String res : ress) {
      try {
        report = this.andonTemplateService.deleteAndonTemplateAndAttributesByPrimaryKey(siteCode, res);
      } catch (Exception e) {
        report = new BatchJobReport(res, e.getMessage(), false);
      }
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }
  
  /**
   * 
   * <strong>Title : validateAndonTemplateCodeExist<br></strong>
   * <strong>Description : </strong>验证ANDON模版的编码是否存在<br> 
   * <strong>Create on : 2016年10月26日 上午10:09:17<br></strong>
   * <p>
   * @param andonTemplateCode ANDON模版编码
   * @param model 模型
   * @return
   * boolean 是否存在
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("validateAndonTemplateCodeExist")
  @ResponseBody
  public boolean validateAndonTemplateCodeExist(String templateCode, Model model) {
    if (!StringUtils.isEmpty(templateCode)) {
      return !this.andonTemplateService.existAndonTemplateByPrimaryKey(this.getSiteCode(), templateCode.toUpperCase());
    }
    return false;
  }

}
